package com.start.workflow.core.dispatcher;

import com.start.common.std.StartEventDispatcherListener;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.workflow.core.event.StartWorkFlowInstanceEvent;
import com.start.workflow.core.event.StartWorkFlowModelEvent;
import com.start.workflow.model.StartProcessModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 模型事件分发器
 */
@Slf4j
public class StartGlobalActModelEventDispatcher extends StartEventDispatcherListener<StartGlobalActModelEventDispatcher, StartWorkFlowModelEvent> implements StartWorkFlowModelEvent {
    public static final String GROUP_PREFIX = "group_type";
    public static final String TABLE_NAME_PREFIX = "proc_def_key";
    public static final String GLOBAL_PREFIX = "global_type";
    public static final String GLOBAL_KEY = "global_key";


    /**
     * 初始化分发器
     */
    @Override
    protected void initDispatcher() {
        Map<String, StartWorkFlowModelEvent> beans = SpringContextTools.getBeans(StartWorkFlowModelEvent.class, false);
        if (beans == null || beans.isEmpty()) {
            return;
        }
        Collection<StartWorkFlowModelEvent> values = beans.values();
        for (StartWorkFlowModelEvent listener : values) {
            if (StringUtils.isNotBlank(listener.key())) {
                addEvent(TABLE_NAME_PREFIX, listener.key(), listener);
                continue;
            }
            if (StringUtils.isNotBlank(listener.categoryKey())) {
                addEvent(GROUP_PREFIX, listener.categoryKey(), listener);
                continue;
            }
            addEvent(GLOBAL_PREFIX, GLOBAL_KEY, listener);
        }
    }


    public static StartGlobalActModelEventDispatcher getInstance() {
        return StartSingletonFactory.get(StartGlobalActModelEventDispatcher.class);
    }

    /**
     * 创建流程模型前
     *
     * @param model 流程模型配置
     */
    @Override
    public void beforeCreateModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeCreateModel(model);
            }
        }
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeCreateModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeCreateModel(model);
            }
        }
    }

    /**
     * 创建流程模型后
     *
     * @param model 流程模型配置
     */
    @Override
    public void afterCreateModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterCreateModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterCreateModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterCreateModel(model);
            }
        }
    }

    /**
     * 修改流程模型前
     *
     * @param model 模型配置
     */
    @Override
    public void beforeUpdateModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeUpdateModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeUpdateModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeUpdateModel(model);
            }
        }
    }

    /**
     * 修改流程模型后
     *
     * @param model 模型配置
     */
    @Override
    public void afterUpdateModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterUpdateModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterUpdateModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterUpdateModel(model);
            }
        }
    }

    /**
     * 删除流程模型前
     *
     * @param model
     */
    @Override
    public void beforeDeleteModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
    }

    /**
     * 删除流程模型后
     *
     * @param model 模型id
     */
    @Override
    public void afterDeleteModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeleteModel(model);
            }
        }
    }

    /**
     * 发布流程模型前
     *
     * @param model 模型id
     */
    @Override
    public void beforeDeploymentModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeploymentModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeploymentModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.beforeDeploymentModel(model);
            }
        }
    }

    /**
     * 发布流程模型后
     *
     * @param model 模型id
     */
    @Override
    public void afterDeploymentModel(StartProcessModel model) {
        List<StartWorkFlowModelEvent> events = getEvents(TABLE_NAME_PREFIX, model.getActModelKey());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterDeploymentModel(model);
            }
        }
        events.clear();
        events = getEvents(GROUP_PREFIX, model.getActModelCategory());
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterDeploymentModel(model);
            }
        }
        events = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (!events.isEmpty()) {
            for (StartWorkFlowModelEvent event : events) {
                event.afterDeploymentModel(model);
            }
        }
    }


}
